package com.be.beadmin.device.controller;

import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.device.dto.output.InspectStandardExcelEx;
import com.be.beadmin.device.entity.InspectStandard;
import com.be.beadmin.device.dto.output.InspectStandardEx;
import com.be.beadmin.device.service.InspectStandardCategoryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.device.service.InspectStandardService;
import com.be.beadmin.device.dto.input.InspectStandardQueryCriteria;
import com.be.beadmin.device.dto.input.InspectStandardDto;
import org.springframework.data.domain.Pageable;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.excel.EasyExcel;

/**
* @author zhouzhou
* @date 2023-08-06
**/
@RestController
public class InspectStandardController {

    @Autowired
    private  InspectStandardService inspectStandardService;


    @Autowired
    private InspectStandardCategoryService inspectStandardCategoryService;

    @PostMapping(value = "/device/getInspectStandardByPage")
    public ResultModel getInspectStandardByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(inspectStandardService.getInspectStandardByPage(pageEntity)) ;
    }

    @PostMapping("/device/addInspectStandard")
    public ResultModel addInspectStandard(@RequestBody InspectStandardDto inspectStandardDto){
        inspectStandardService.addInspectStandard(inspectStandardDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/device/delInspectStandard")
    public ResultModel delInspectStandard(@RequestBody InspectStandardDto inspectStandardDto){
        inspectStandardService.delInspectStandard(inspectStandardDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/device/updateInspectStandard")
    public ResultModel updateInspectStandard(@RequestBody InspectStandardDto inspectStandardDto){
        inspectStandardService.updateInspectStandard(inspectStandardDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/device/exportInspectStandard")
    public void exportInspectStandard(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<InspectStandardEx> inspectStandardPage = inspectStandardService.getInspectStandardByPage(pageEntity);
        List<InspectStandardEx> inspectStandardExes = inspectStandardPage.getContent();
        List<InspectStandardExcelEx> inspectStandardExcelExes=inspectStandardExes.stream().map(item->{
            InspectStandardExcelEx excel =new InspectStandardExcelEx();
            BeanUtils.copyProperties(item,excel);
            return excel;
        }).collect(Collectors.toList());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("InspectStandard接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), InspectStandardExcelEx.class).sheet("sheet1").doWrite(inspectStandardExcelExes);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), InspectStandardExcelEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(inspectStandardExcelExes);
    }

    @PostMapping(value = "/device/getInspectStandardByTemplateID")
    public ResultModel getInspectStandardByTemplateID(@RequestParam String TemplateID)  {
        return ResultModel.success(inspectStandardService.getInspectStandardByTemplateID(TemplateID)) ;
    }

    @PostMapping(value = "/device/getInspectStandardByPageTemplateId")
    public ResultModel getInspectStandardByPageTemplateId(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(inspectStandardService.getInspectStandardByPage(pageEntity)) ;
    }

    @ApiOperation("获取TemplateIdlist")
    @PostMapping("/device/getTemplateIdlist")
    public ResultModel getTemplateIdlist(){
//        List<String> TemplateIdlist = inspectStandardCategoryService.getTemplateId();
        Map<String, String> TemplateNamelist = inspectStandardCategoryService.getTemplateIdAndNames();
        return ResultModel.success(TemplateNamelist);
    }

    @ApiOperation("获取全部检查项目")
    @PostMapping("/device/getCheckItem")
    public ResultModel getCheckItem(){
        return ResultModel.success(inspectStandardService.getCheckItem());
    }

    @ApiOperation("根据检查项目获取检查描述")
    @PostMapping("/device/getCheckDesc")
    public ResultModel getCheckDescByCheckItem(@RequestBody String CheckItem){
        return ResultModel.success(inspectStandardService.getCheckDescByCheckItem(CheckItem));
    }
}